<?php
namespace Api\Model;
use Think\Model\RelationModel;

class CustomerModel extends RelationModel
{
    protected $_link = array(
        'FamilyMember' => array(
            'mapping_type' => self::HAS_MANY,
            'class_name' => 'FamilyMember',
            'mapping_name' => 'family_member',
            'foreign_key' => 'customer_id'
        ),
        'Family' => array(
            'mapping_type' => self::MANY_TO_MANY,
            'class_name' => 'Family',
            'mapping_name' => 'families',
            'foreign_key' => 'customer_id',
            'relation_foreign_key' => 'family_id',
            'relation_table' => 'zh_family_member'
        )
    );

    public function attestation($wxcus_id){
        $info = D('Admin/WxuserInfo')->where(array('customer_id'=>$wxcus_id))->find();
        $user = D('Admin/Wxuser')->find($wxcus_id);
        $sex = ($info['sex'] == 1? "M":"F");
        $customerData = $this->findCustomer($user['name'],$info['tel'],$sex,$wxcus_id,$info['type']);
        return $customerData;
    }
    
    public function findCustomer($name,$tel,$sex,$wxcus_id,$type){
        $customerData1 = $this->where(array('name'=>$name, 'tel'=>$tel, 'status'=>1))->find();
        $customerData2 = $this->where(array('name'=>$name, 'attestation_tel'=>$tel, 'status'=>1))->find();
        $customerData = $customerData1?$customerData1:$customerData2;

        $old = $this->where(array('attestation_id'=>$wxcus_id))->find();
         if($old){
             $old['type'] = $type;
             $_data = array();
             $_data['name'] = $name;
             $_data['attestation_tel'] = $tel;
             $_data['sex'] = $sex;
             $this->data($_data)->where(array('id' => $old['id']))->save();
             return $old;
        }elseif($customerData){
             $_data = array();
             $_data['name'] = $name;
             $_data['attestation_tel'] = $tel;
             $_data['attestation_id'] = $wxcus_id;
             $_data['sex'] = $sex;
             $this->data($_data)->where(array('id' => $customerData['id']))->save();
        }else {
             $customerData = $this->createCustomer($name, $tel, $sex, $wxcus_id);
         }
        $customerData['type'] = $type;
        return $customerData;
    }

    public function findFamily($room_id){
        $familyData = M('Family')->where(array('room_id'=>$room_id, 'status'=>1))->find();
        $fid = $familyData['id'];
        if(!$familyData) {
            $_data = array();
            $_data['room_id'] = $room_id;
            $_data['no'] = 11;
            $_data['status'] = 1;
            $_data['create_time'] = time();
            $_data['modify_time'] = time();
            $fid = D('Admin/Family')->data($_data)->add();
        }
        return $fid;
    }

    public function createCustomer($name,$tel,$sex,$wxcus_id){
        $_data = array();
        $_data['tel'] = $tel;
        $_data['attestation_tel'] = $tel;
        $_data['name'] = $name;
        $_data['sex'] = 'M';
        $_data['ugroup'] = 1;
        $_data['status'] = 1;
        $_data['sex'] = $sex;
        $_data['creator_id'] = 5;
        $_data['modifier_id'] = 5;
        $_data['deleted'] = 0;
        $_data['hot'] = 0;
        $_data['attestation_id'] = $wxcus_id;
        $_data['avatar'] = 0;
        $_data['create_time'] = time();
        $_data['modify_time'] = time();
        $_data['customer_no'] = D('Admin/Customer')->getCustomerId();
        $id = D('Admin/Customer')->data($_data)->add();
        return D('Customer')->find($id);
    }

    public function deleteFamilyMember($id){
        $wxcsData = M('QtwyCustomer','tp_','DB_WX')->find($id);
        M('FamilyMember')->where(array(
            'room_id'=>$wxcsData['room_id'],
            'customer_id'=>$wxcsData['crm_id']
        ))->delete();
    }

    public function findMember($fid,$cusData){
        $has = M('FamilyMember')->where(array('family_id'=>$fid, 'customer_id'=>$cusData['id']))->find();
        $hasOld = M('FamilyMember')->where(array('attestation_id'=>$cusData['attestation_id']))->find();

        $_data = array();
        if($cusData['type']==1)
            $_data['prefix'] = '成员';
        elseif($cusData['type']==2)
            $_data['prefix'] = '户主';
        elseif($cusData['type']==3)
            $_data['prefix'] = '租客';

        if ($_data['prefix'] == '户主') {
            M('FamilyMember')->where(array('family_id' => array('eq', $fid), 'customer_id' => array('neq', $cusData['id']), 'prefix'=>'户主'))->save(array('prefix' => '成员'));
            M('Room')->data(array('householder_name'=>$cusData['name'],'householder_mobi'=>$cusData['attestation_tel']))->save();
        }

        if ($has && !$hasOld) {
            $_data['name'] = $cusData['name'];
            $_data['attestation_id'] = $cusData['attestation_id'];
            $_data['modify_time'] = time();
            $chengyuan = M('FamilyMember')->where(array('id'=>$has['id']))->data($_data)->save();
        } else {
            M('FamilyMember')->where(array('attestation_id'=>$cusData['attestation_id']))->delete();
            $_data['family_id'] = $fid;
            $_data['customer_id'] = $cusData['id'];
            $_data['name'] = $cusData['name'];
            $_data['attestation_id'] = $cusData['attestation_id'];
            $_data['create_time'] = time();
            $_data['modify_time'] = time();
            $chengyuan = M('FamilyMember')->data($_data)->add();
        }
    }



    protected function _before_insert(&$data, $options)
    {
        parent::_before_insert($data, $options); // TODO: Change the autogenerated stub
//        if(!$data['birthday'] && $data['id_no'] != ''){
//            $data['birthday'] = getBirthday($data['id_no']);
//        }
    }

    protected function _after_select(&$resultSet, $options)
    {
        parent::_after_select($resultSet, $options); // TODO: Change the autogenerated stub
//        D('Dict')->dictTransform($resultSet,array('ugroup'));
    }
}
